package com.yjhb.android.sbz.mvp.export

import com.alibaba.fastjson.JSONObject
import com.yjhb.android.sbz.entity.ExportData
import com.yjhb.android.sbz.http.HttpObserver
import com.yjhb.android.sbz.http.HttpTool
import com.yjhb.android.sbz.mvp.ConventionPresenter
import com.jojo.android.zxlib.converter.FastJsonConverterFactory
import java.util.ArrayList


/**
 *  创建者 zx
 *  创建时间 2021/3/17 17:50
 */
class ExportPresenter(v: ExportView?) : ConventionPresenter<ExportView>(v) {
    override fun onDestroy() {
    }

    override fun converterFactory() = FastJsonConverterFactory()


    fun successDataToExcel(taskId: Int, start: String, end: String) {
        view?.onWait("正在处理...")
        HttpTool.request(model.successDataToExcel(taskId, start, end), object : HttpObserver<Any?>() {
            override fun onResult(data: Any?) {
                view?.let {
                    it.onSubmitSuccess()
                }
            }

            override fun onError(code: Int, msg: String?) {
                view?.let {
                    it.onFailure(code, msg)
                }
            }
        })
    }


    fun exportInfo(taskId: Int) {
        view?.onWait("请稍候...")
        HttpTool.request(model.exportInfo(taskId), object : HttpObserver<JSONObject?>() {
            override fun onResult(data: JSONObject?) {
                data?.let {
                    view?.onDataList(it.getJSONArray("list").toJavaList(ExportData::class.java) as ArrayList<ExportData>)
                    view?.onLastCount(it.getIntValue("num"))
                }
            }

            override fun onError(code: Int, msg: String?) {
                view?.onFailure(code, msg)
            }
        })

    }

}